<?php

/**
 * Description of works_model
 *
 * @author ptnhuan
 */
class Works_model extends CI_Model {

    public $top_thumb_works = 2;
    public $top_works = 7;
    public $top_thumb_works_thuctap = 3;
    public $top_works_thuctap = 7;
    public $works_recent = 5;
    public $works_popular = 5;
    public $show_page = 10;
    private $_table = "works";

    public function __construct() {
        
    }

    public function get_works_where($params = NULL, $counter = false) {
        $where =array();
        // Where for work;
        if (!empty($params['id']))
            $where['id']=$params['id'];
        if (isset($params['id_site']))
            $where['id_site']=$params['id_site'];
        //TODO (Chưa đủ điều kiện cho keyword)
        if (!empty($params['keyword']))
            $where .= " AND (description LIKE '% {$params['keyword']} %')";
        $query = $this->db->get_where($this->_table, $where);
        if (!$counter)
        return $query->result_array();
        else
        return $query->num_rows();
    }

    public function get_top_works($is_thumb_works = FALSE) {
        $query = NULL;
        if ($is_thumb_works) {
            $query = $this->db->get($this->_table, $this->top_thumb_works, 0);
        } else {
            $query = $this->db->get($this->_table, $this->top_works, 0);
        }
        return $query->result_array();
    }

    public function get_works_thuctap($is_thumb_works = FALSE) {
        $query = NULL;
        if ($is_thumb_works)
            $query = $this->db->get($this->_table, $this->top_thumb_works_thuctap, 0);
        else
            $query = $this->db->get($this->_table, $this->top_works_thuctap, 0);
        return $query->result_array();
    }

    public function get_works_recent() {
        $query = $this->db->get($this->_table, $this->works_recent, 0);
        return $query->result_array();
    }

    public function get_works_popular() {
        $query = $this->db->get($this->_table, $this->works_popular, 0);
        return $query->result_array();
    }

    public function insert_works($params = array()) {
        return $this->db->insert($this->_table, $params);
    }
    public function update_works($params = array(),$id) {
        if(isset($id)){
            $where='id='.$id;
            return $this->db->update_string($this->_table,$params,$where);
        }
        else{
            return FALSE;
        }
    }
    public function delete_works($id) {
        if(isset($id)){
            return $this->db->delete($this->_table,array('id'=>$id));
        }
        else{
            return FALSE;
        }
    }
    public function get_works($params=array(),$counter=false){
        $where=array();
        if(isset($params['type']))
            $where['type']=(int)$params['type'];
        if(isset($params['categories']))
            $where['id_categories']=(int) $params['categories'];
        if(!$counter){
            $query=$this->db->get_where($this->_table,$where,PER_PAGE, $params['page']);
            //var_dump($this->db->last_query());
            return $query->result_array();
        }
        else{
            $query=$this->db->get_where($this->_table,$where);
            return $query->num_rows();
        }
    }
    
}
